﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using DAL;
using DACNPM.MyUserControl.UCHienThi;

namespace DACNPM.MyUserControl.UCHienThi
{
    public partial class UCThemHang : UserControl
    {
        NhaCungCapBUO NCCBUO = new NhaCungCapBUO();
        HangHoaBUO haBUO = new HangHoaBUO();
        NumberBUO numBUO = new NumberBUO();
        String idHa;
        int id;

        public UCThemHang()
        {
            InitializeComponent();
            showComboboxNCC();
            dataGridView1.DataSource = haBUO.GetAll();
        }

        private void btnLamLai_Click(object sender, EventArgs e)
        {
            txtTenHang.Text = "";
            txtDonGia.Text = "";
            cbxNCC.Text = "";
        }

        void showComboboxNCC()
        {
            cbxNCC.DataSource = NCCBUO.GetAll();
            cbxNCC.DisplayMember = "TenNhaCungCap";
            cbxNCC.ValueMember = "id_NhaCungCap";
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if ((txtTenHang.Text == "") || (txtDonGia.Text == "") || (cbxNCC.Text == ""))
            {
                MessageBox.Show("Chưa nhập đủ thông tin cần thiết!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                if (numBUO.isNumberInput(txtDonGia.Text) == false)
                {
                    MessageBox.Show("Trường đơn giá phải nhập số!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    tbl_Hang ha = new tbl_Hang();
                    ha.TenHang = txtTenHang.Text;
                    ha.DonGia = int.Parse(txtDonGia.Text);
                    ha.NSX = DateTime.Parse(dateTimePicker1.Text);

                    if (haBUO.Insert(ha) == true)
                    {
                        txtDonGia.Text = "";
                        txtTenHang.Text = "";
                        cbxNCC.Text = "";

                        tbl_NhaCungCap ncc = (tbl_NhaCungCap)cbxNCC.SelectedItem;
                        //String a = haBUO.getIDNCC(ncc.TenNhaCungCap);

                        tbl_NCC_Hang ncch = new tbl_NCC_Hang();
                        ncch.id_Hang = haBUO.getNewIDHang();
                        ncch.id_NhaCungCap = haBUO.getIDNCC(ncc.TenNhaCungCap);

                        if (haBUO.InsertNCC_Hang(ncch) == true)
                        {
                            dataGridView1.DataSource = haBUO.GetAll();
                            MessageBox.Show("Thêm hàng hóa thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            UCThemHang ucThemHang = new UCThemHang();
                            Form form = this.ParentForm;
                            ControlAccess.ShowConTrol(form, ucThemHang, "grbHienThiChucNang");
                        }
                        else
                        {
                            MessageBox.Show("Lỗi khi thêm vào bảng tbl_NCC_Hang", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    }
                    else
                    {
                        MessageBox.Show("Đã xảy ra lỗi khi thêm hàng hóa thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }

                }
            }

            //tbl_NhaCungCap ncc = (tbl_NhaCungCap)cbxNCC.SelectedItem;
            //String a = haBUO.getIDNCC(ncc.TenNhaCungCap);

            //MessageBox.Show(cbxNCC.SelectedItem.ToString());
        }

        private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                if (e.Button == System.Windows.Forms.MouseButtons.Right)
                {

                    DataGridView.HitTestInfo hit = dataGridView1.HitTest(e.X, e.Y);


                    // Only RowHeader and Cells (which make up a row) should fire a menu
                    if (hit.Type == DataGridViewHitTestType.RowHeader
                        || hit.Type == DataGridViewHitTestType.Cell
                        && hit.RowIndex >= 0)
                    {
                        // Notice how I assign the menu to the dataGridView below...

                        dataGridView1.ClearSelection();
                        dataGridView1.Rows[hit.RowIndex].Selected = true;
                        dataGridView1.ContextMenuStrip = contextMenuStrip1;
                        dataGridView1.ContextMenuStrip.Show(this.dataGridView1, new Point(e.X, e.Y));

                        id = dataGridView1.HitTest(e.X, e.Y).RowIndex;
                        idHa = dataGridView1.Rows[id].Cells[0].Value.ToString();


                    }
                    // and then I remove the menu here...
                    // This is because after we assign it to the dataGridView
                    // it allows you to right-click anywhere on the control to
                    // get a menu.  Try removing this line and expirement.
                    dataGridView1.ContextMenuStrip = null;

                }
            }
            catch
            {
                contextMenuStrip1.Visible = false;
            }
        }

        private void xemDanhSáchHàngHóaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UCDanhSachHang ucDSH = new UCDanhSachHang();
            Form form = this.ParentForm;

            ControlAccess.ShowConTrol(form, ucDSH, "grbHienThiChucNang");
        }

        private void sửaHàngHóaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UCSuaHang ucSuaHang = new UCSuaHang(idHa);
            Form form = this.ParentForm;

            ControlAccess.ShowConTrol(form, ucSuaHang, "grbHienThiChucNang");
        }

        private void xóaHàngHóaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult ds = MessageBox.Show("Bạn có chắc chắn muốn xóa", "Xóa dữ liệu hàng hóa", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (ds == DialogResult.Yes)
            {
                if (haBUO.Delete(idHa))
                {
                    MessageBox.Show("Xóa hàng hóa thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("Xóa hàng hóa thất bại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                dataGridView1.DataSource = haBUO.GetAll();
            }
        }
    }
}
